/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lfk.demo.Ability;

import com.lfk.EventHelper;
import com.lfk.demo.MainAbility;
import com.lfk.justwetools.Component.ReadView.ReadView;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.miscservices.timeutility.Time;
import ohos.multimodalinput.event.TouchEvent;
import ohos.utils.net.Uri;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class ReadAbilityTest {

    public static final String READ_FILE_NAME = "exm.txt";
    public static String FILE_PATH;

    private static IAbilityDelegator sAbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
    private MainAbility mainAbility;
    private ReadAbility ability;
    private ReadView readView;

    @Before
    public void setUp() throws Exception {
        mainAbility = EventHelper.startAbility(MainAbility.class);
        EventHelper.waitForActive(mainAbility, 5);
        Thread.sleep(2000);

        Intent intent = new Intent();
        Intent.OperationBuilder builder = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(mainAbility.getBundleName());
        mainAbility.copytext(READ_FILE_NAME, 1);
        FILE_PATH = mainAbility.getDataDir() + "/bin";
        builder.withUri(Uri.parse(FILE_PATH + "/" + READ_FILE_NAME));
        builder.withAbilityName(ReadAbility.class);
        intent.setOperation(builder.build());

        ability = EventHelper.startAbility(ReadAbility.class, intent);
        Thread.sleep(3000);
    }

    @After
    public void tearDown() throws Exception {
        Thread.sleep(500);
        sAbilityDelegator.stopAbility(ability);
        EventHelper.clearAbilities();
    }

    @Test
    public void testForward() throws Exception {
        readView = (ReadView) ability.findComponentById(0xffff001);
        int[] src = readView.getPosition();

        long time = Time.getRealActiveTime();
        Point screenSize = new Point();
        DisplayManager.getInstance().getDefaultDisplay(ability).get().getSize(screenSize);
        TouchEvent ev1 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_DOWN, screenSize.getPointX() - 100, screenSize.getPointY() - 200);
        TouchEvent ev2 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_UP, screenSize.getPointX() - 100, screenSize.getPointY() - 200);
        sAbilityDelegator.triggerTouchEvent(ability, ev1);
        sAbilityDelegator.triggerTouchEvent(ability, ev2);
        Thread.sleep(1000);

        int[] dst = readView.getPosition();
        assertTrue("src[0]:" + src[0] + ", dst[0]:" + dst[0], dst[0] > src[0]);
    }

    @Test
    public void testBackwards() throws Exception {
        readView = (ReadView) ability.findComponentById(0xffff001);

        {
            long time = Time.getRealActiveTime();
            Point screenSize = new Point();
            DisplayManager.getInstance().getDefaultDisplay(ability).get().getSize(screenSize);
            TouchEvent ev1 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_DOWN, screenSize.getPointX() - 100, screenSize.getPointY() - 200);
            TouchEvent ev2 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_UP, screenSize.getPointX() - 100, screenSize.getPointY() - 200);
            sAbilityDelegator.triggerTouchEvent(ability, ev1);
            sAbilityDelegator.triggerTouchEvent(ability, ev2);
            Thread.sleep(1000);
        }

        int[] src = readView.getPosition();

        {
            long time = Time.getRealActiveTime();
            Point screenSize = new Point();
            DisplayManager.getInstance().getDefaultDisplay(ability).get().getSize(screenSize);
            TouchEvent ev1 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_DOWN, 100, screenSize.getPointY() - 200);
            TouchEvent ev2 = EventHelper.obtainTouchEvent(time, time, EventHelper.ACTION_UP, 100, screenSize.getPointY() - 200);
            sAbilityDelegator.triggerTouchEvent(ability, ev1);
            sAbilityDelegator.triggerTouchEvent(ability, ev2);
            Thread.sleep(1000);
        }

        int[] dst = readView.getPosition();
        assertTrue("src[0]:" + src[0] + ", dst[0]:" + dst[0], dst[0] < src[0]);
    }
}